package mkis.service.root.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mkis.service.root.common.PageInfo;
import mkis.service.root.common.SearchInfo;
import mkis.service.root.data.model.Member;
import mkis.service.root.data.model.QuestionsDt;
import mkis.service.root.data.model.TeamTouch;
import mkis.service.root.data.model.TeamTouchDetail;
import mkis.service.root.data.model.TeamtouchCust;
import mkis.service.root.data.model.TeamtouchIdea;
import mkis.service.root.service.MemberService;
import mkis.service.root.service.TeamTouchService;
import mkis.service.root.utils.MkisUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TeamTouchCTR {
	@Resource(name="teamTouchService")
	private TeamTouchService teamTouchService;
	
	@Resource(name="mkisUtils")
	private MkisUtils mkisUtils;
	
	@Resource(name="memberService")
	private MemberService memberService;
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@RequestMapping(value="/teamt/teamt_list.do")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,PageInfo pageInfo, SearchInfo searchInfo,String sYear,String sMonth,String listMode,String mb_dept_code,String mb_team_code) throws Exception {
		ModelAndView mav=new ModelAndView("teamt/teamt_list");
		
		
		
		
		
		String page=pageInfo.getPage();
		
		if(page==null || "".equals(page)){
			page="1";
		}
		
		int rowcnt=pageInfo.getRowcnt();
		int startRow=(Integer.parseInt(page)-1)*rowcnt;
		String input=searchInfo.getInput();
		String typeSelect=searchInfo.getTypeSelect();
		
		Date date=new Date(System.currentTimeMillis());
		SimpleDateFormat sf=new SimpleDateFormat("yyyy");
		
		Map<String,Object> paramMap=mkisUtils.makeListParam(page, startRow, rowcnt, pageInfo, searchInfo);
		
		
		Member ss_mb = MkisUtils.getMember(request);
		
		paramMap.put("mb_id", ss_mb.getMb_id());
		
		
		int mb_auth_code=ss_mb.getMb_auth_code();
		if(listMode==null || "".equals(listMode) ||"person".equals(listMode)){
			listMode="person";
		} else {
			if(mb_auth_code==1 || mb_auth_code==6) {
				//list TODO
			} else {
				if(mb_auth_code==2) {
					mb_dept_code=String.valueOf(ss_mb.getMb_dept_code());
					mb_team_code=String.valueOf(ss_mb.getMb_team_code());
					
					paramMap.put("mb_dept_code", ss_mb.getMb_dept_code());
					paramMap.put("mb_team_code", ss_mb.getMb_team_code());
				} else if(mb_auth_code==3) {
					mb_dept_code=String.valueOf(ss_mb.getMb_dept_code());
					
					paramMap.put("mb_dept_code", ss_mb.getMb_dept_code());
					paramMap.put("mb_team_code", mb_team_code);
				} else if(mb_auth_code==4 || mb_auth_code==5 || mb_auth_code==7 || mb_auth_code==8) {
					paramMap.put("mb_dept_code", mb_dept_code);
				} 
				
			}
			//listMode="multi";
		}
		
		paramMap.put("listMode", listMode);
		
		String yymm=null;
		
		if(sYear!=null &&!"".equals(sYear)) {
			yymm=sYear;
		}
		
		if(sMonth!=null &&!"".equals(sMonth)) {
			
			int sM=Integer.parseInt(sMonth);
			
			if(sM<=9) {
				sMonth="0"+sM;
			} 
			
			yymm=yymm+"-"+sMonth;
		}
		
		paramMap.put("yymm", yymm);
		
		int totalcnt=teamTouchService.getTeamTouchCount(paramMap);
		List<TeamTouch> list=teamTouchService.getTeamTouchList(paramMap);
		String modeText=null;
		
		Map<String, Object> isEditParam = new HashMap<String, Object>();
		isEditParam.put("teamtTouchDay", new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis())));
		isEditParam.put("mb_id", MkisUtils.getMember(request).getMb_id());
		
		int cnt=teamTouchService.getTeamTouchViewCount(isEditParam);
		if(cnt>0){
			modeText="수정";
		} else {
			modeText="등록";
		}
		
		mav.addObject("modeText", modeText);
		
		
		mav.addObject("list", list);
		mav.addObject("page", Integer.parseInt(page));
		mav.addObject("totalcnt", totalcnt);
		
		mav.addObject("sYear", sYear);
		mav.addObject("endYear", Integer.parseInt(sf.format(date)));
		
		mav.addObject("sMonth", sMonth);
		mav.addObject("input", input);
		mav.addObject("typeSelect", typeSelect);
		
		mav.addObject("listMode", listMode);
		
		mav.addObject("mb_dept_code",mb_dept_code);
		mav.addObject("mb_team_code",mb_team_code);
		
		return mav;
	}
	
	@RequestMapping(value="/teamt/teamt_edit.do", method=RequestMethod.GET)
	public ModelAndView editForm(HttpServletRequest request,HttpServletResponse response,String paramDay,String mb_id) throws Exception {
		ModelAndView mav=new ModelAndView("teamt/teamt_edit");
		
		if(paramDay==null ||"".equals(paramDay)) {
			paramDay=new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
		}
		
		String mode=null;
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("teamtTouchDay", paramDay);
		paramMap.put("mb_id", mb_id);
		
		int cnt=teamTouchService.getTeamTouchViewCount(paramMap);
		if(cnt>0){
			mode="update";
		} else {
			mode="insert";
		}
		
		List<TeamTouch> qnaList=teamTouchService.getTeamTouchView(paramMap);
		List<TeamtouchIdea> ideaList=teamTouchService.getIdeaList(paramMap);
		
		List<QuestionsDt> questionsList=teamTouchService.getQuestionsList(paramMap);
		List<TeamtouchCust> contactCustList=teamTouchService.getContactCustList(paramMap);
		
		mav.addObject("questionsList", questionsList);
		mav.addObject("contactCustList", contactCustList);
		mav.addObject("ideaList", ideaList);
		mav.addObject("mode", mode);
		mav.addObject("qnaList", qnaList);
		mav.addObject("paramDay",paramDay);
		return mav;
	}
	
	@RequestMapping(value="/teamt/teamt_edit.do", method=RequestMethod.POST)
	public ModelAndView submit(HttpServletRequest request,HttpServletResponse response,String paramDay,String mb_id,String mode) throws Exception {
		ModelAndView mav=new ModelAndView("teamt/teamt_edit_complete");
		
		Map<String,Object> paramMap=new HashMap<String, Object>();
		paramMap.put("mode", mode);
		paramMap.put("paramDay", paramDay);
		paramMap.put("mb_id", MkisUtils.getMember(request).getMb_id());
		
		
		for(int a=1;a<=5;a++) {
			String bizNoKey="bizNo"+a;//bizNo : common 
			String bizNameKey="bizName"+a;//bizNo : common
			
			String bizNo=request.getParameter(bizNoKey);//bizNo : 1,2,4
			String bizName=request.getParameter(bizNameKey);//bizNo : 1,2,4
			
			paramMap.put(bizNoKey, bizNo);//common
			paramMap.put(bizNameKey, bizName);//common
			
			if(a!=3 && a!=5){
				for(int b=1;b<=4;b++) {
					String answerKey="answer"+a+b;//bizNo : 1,2,4
					String answer=request.getParameter(answerKey);//bizNo : 1,2,4
					paramMap.put(answerKey, answer);
				}
			} else if(a==3 && a!=5){
				for(int b=1;b<=3;b++) {
					String custNameKey="cust_name"+a+b;//bizNo : 3
					String birthdayDateKey="birthdayDate"+a+b;//bizNo : 3
					String custName=request.getParameter(custNameKey);
					String birthdayDate=request.getParameter(birthdayDateKey);
					paramMap.put(custNameKey, custName);
					paramMap.put(birthdayDateKey, birthdayDate);
				}
			} else {
				String personScoreKey="personScore"+a;
				String personScore=request.getParameter(personScoreKey);
				paramMap.put(personScoreKey, personScore);
			}
		}
		
		logger.debug("paramMap : {} ",paramMap);
		teamTouchService.multiSaveData(paramMap);
		
		return mav;
	}
	
	@RequestMapping(value="/teamt/teamt_view.do")
	public ModelAndView viewForm(HttpServletRequest request,HttpServletResponse response,String paramDay,String mb_id, String page, String sYear, String sMonth, String typeSelect, String input, String listMode) throws Exception {
		ModelAndView mav=new ModelAndView("teamt/teamt_view");
		
		if(paramDay==null ||"".equals(paramDay)) {
			paramDay=new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
		}
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("teamtTouchDay", paramDay);
		paramMap.put("mb_id", mb_id);
	
		String mode=null;
		
		int cnt=teamTouchService.getTeamTouchViewDtCount(paramMap);
		if(cnt>0){
			mode="update";
		} else {
			mode="insert";
		}
		
		List<QuestionsDt> questionsList=teamTouchService.getQuestionsList(paramMap);
		List<TeamtouchCust> contactCustList=teamTouchService.getContactCustList(paramMap);
		List<TeamtouchIdea> ideaList=teamTouchService.getIdeaList(paramMap);
		Member ss_mb=MkisUtils.getMember(request);
		String ss_mb_auth_code=String.valueOf(ss_mb.getMb_auth_code());
		String ss_mb_auth_level=String.valueOf(ss_mb.getMb_auth_level());
		
		Member reg_mb=memberService.getMember(paramMap);
		String reg_auth_code=String.valueOf(reg_mb.getMb_auth_code());
		String reg_auth_level=String.valueOf(reg_mb.getMb_auth_level());
		
		String reg_authType=teamTouchService.getAuthType(ss_mb_auth_code,ss_mb_auth_level,reg_auth_code,reg_auth_level,paramDay);
		
		List<TeamTouch> qnaList=teamTouchService.getTeamTouchView(paramMap);
		TeamTouchDetail detail = teamTouchService.getTeamTouchDetail(paramMap);
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date(System.currentTimeMillis());
		String today=sf.format(date);
		mav.addObject("mb_id", mb_id);
		mav.addObject("authType", reg_authType);
		mav.addObject("qnaList", qnaList);
		mav.addObject("questionsList", questionsList);
		mav.addObject("contactCustList", contactCustList);
		mav.addObject("ideaList", ideaList);
		mav.addObject("detail", detail);
		mav.addObject("paramDay",paramDay);
		mav.addObject("today",today);
		mav.addObject("mode",mode);
		
		//list
		mav.addObject("page", page);
		mav.addObject("sYear", sYear);
		mav.addObject("sMonth", sMonth);
		mav.addObject("typeSelect", typeSelect);
		mav.addObject("input", input);
		mav.addObject("listMode", listMode);
		
		
		return mav;
	}
	@RequestMapping(value="/teamt/teamt_answer.do", method=RequestMethod.POST)
	public ModelAndView answerForm(HttpServletRequest request,HttpServletResponse response, String listMode, String page, String sYear, String sMonth, String typeSelect, String input) throws Exception {
		ModelAndView mav=new ModelAndView("teamt/teamt_answer_complete");
		
		int result=teamTouchService.multiSaveAnswerData(request);
		mav.addObject("listMode", listMode);
		mav.addObject("page", page);
		mav.addObject("sYear", sYear);
		mav.addObject("sMonth", sMonth);
		mav.addObject("typeSelect", typeSelect);
		mav.addObject("input", input);
		return mav;
	}
	
	@RequestMapping(value="/teamt/teamt_answer_chk.do")
	public @ResponseBody String answerchk(HttpServletRequest request,HttpServletResponse response,String mb_id,String paramDay) throws Exception {
		String code=null;
		Map<String, Object> paramMap=new HashMap<String, Object>();
		paramMap.put("mb_id", mb_id);
		paramMap.put("paramDay", paramDay);
		
		int cnt=teamTouchService.getAnswerChk(paramMap);
		
		if(cnt>0) {
			code="0000";
		} else {
			code="0001";
		}
		
		return code;
	}
	
}
